package com.wyzz.hr.action;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.IAttendshiftBO;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.bo.IEmpshiftBo;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.pojo.Attendperiod;
import com.wyzz.hr.pojo.Attendshift;
import com.wyzz.hr.pojo.Attendshiftareadept;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empshift;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.StringUtil;
import com.wyzz.hr.util.SysConfigManager;
import com.wyzz.hr.util.output.FieldOperate;

public class ExaminScheduleAction extends BaseDownloadAction implements Status {

	public ExaminScheduleAction() {
		scheduleList = null;
		empList = null;
		page = new Pager();
		emp = null;
		employee = null;
		yearMonth = DateUtil.formatTodayToS("yyyy-MM");
		dayList = null;
		operation = null;
		operationId = null;
		shiftList = null;
		defaultDate = DateUtil.parseDateByFormat("20000101", "yyyyMMdd");
		attdShiftBo = (IAttendshiftBO) getBean("attendshiftBO");
		dayFrom = null;
		dayTo = null;
		dayFrom1 = null;
		dayTo1 = null;
		monthFlag = null;
		dayFlag = null;
		flag = "";
		outputIoName = "OExaminShift";
		resultStatus = 1;
	}

	public String execute() throws Exception {
		execParameter();
		getDrillDownList();
		empList = searchEmp();
		System.out.println(empList.size());
		for (int i = 0; i < empList.size(); i++)
			System.out.println(empList.size());
		scheduleList = searchAttendSchedule();
		if ("export".equals(searchOrExport)) {
			generateEmpshiftList();
			List titleInfoList = generateTitleList();
			return downloadToExcel(empList, outputIoName, null, "",
					titleInfoList);
		}
		shiftList = attdShiftBo.searchAttendShift(null, new Integer(1));
		Attendperiod period = attdShiftBo.getCurrentPeriod(yearMonth
				.replaceAll("-", ""));
		if (period != null
				&& period.getAttpStatus() != null
				&& (period.getAttpStatus().intValue() == 1 || period
						.getAttpStatus().intValue() == 2))
			flag = "false";
		else
			flag = "true";
		return "success";
	}

	private List searchEmp() {
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class, "emp");
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 4);
		addCriteria(dc);
		addOrders(dc, null, new String[] { (new StringBuilder()).append("emp.")
				.append(Employee.PROP_EMP_NAME).append("-up").toString() });
		page.splitPage(dc);
		if ("export".equals(searchOrExport))
			return attdShiftBo.searchEmployee(null, dc);
		else
			return attdShiftBo.searchEmployee(page, dc);
	}

	private List searchAttendSchedule() {
		DetachedCriteria dc = DetachedCriteria.forClass(Empshift.class);
		dc.setFetchMode("empshiftEmpNo", FetchMode.JOIN);
		dc.setFetchMode("empshiftShiftNo", FetchMode.JOIN);
		dc.add(Restrictions.lt(Empshift.PROP_EMPSHIFT_DATE, dayTo));
		dc.add(Restrictions.ge(Empshift.PROP_EMPSHIFT_DATE, dayFrom));
		if (empList != null && empList.size() > 0)
			dc.add(Restrictions.in(Empshift.PROP_EMPSHIFT_EMP_NO, empList));
		dc.addOrder(Order.asc(Empshift.PROP_EMPSHIFT_EMP_NO));
		return attdShiftBo.searchAttendSchedule(page, dc);
	}

	private void getDrillDownList() {
		IEmpTypeBO emptypebo = (IEmpTypeBO) SpringBeanFactory
				.getBean("emptypeBO");
		empTypes = emptypebo.FindEnabledEmpType();
		ILocationBO localbo = (ILocationBO) SpringBeanFactory
				.getBean("locationBO");
		locations = localbo.FindEnabledLocation();
		IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		departs = deptbo.FindEnabledDepartment();
	}

	private void execParameter() {
		SysConfigManager dbConfigManager = DatabaseSysConfigManager
				.getInstance();
		Map dbMap = dbConfigManager.getProperties();
		String configValue = (String) dbMap.get("sys.examin.month.sum");
		String configInit[] = { "1", "1" };
		String config[] = configValue == null || configValue.equals("")
				|| configValue.indexOf("-") <= -1 ? configInit : configValue
				.split("-");
		monthFlag = config[0];
		dayFlag = config[1];
		String year = "";
		String month = "";
		if (StringUtils.isEmpty(yearMonth) || yearMonth.length() != 7) {
			year = DateUtil.formatDateToS(new Date(), "yyyy");
			month = DateUtil.formatDateToS(new Date(), "MM");
		} else {
			year = yearMonth.substring(0, 4);
			month = yearMonth.substring(5, 7);
		}
		Map map = getEndDay(year, month);
		dayList = (List) map.get("tmpList");
		dayFrom = (Date) map.get("dayFrom");
		dayTo = (Date) map.get("dayTo");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dayTo);
		calendar.add(5, -1);
		dayFrom1 = DateUtil.formatDateToS(dayFrom, "yyyy-MM-dd");
		dayTo1 = DateUtil.formatDateToS(calendar.getTime(), "yyyy-MM-dd");
		if (employee == null)
			employee = new Employee();
	}

	private void addCriteria(DetachedCriteria dc) {
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empOrgDept", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), "empOrgLoc", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_PB_NO).toString(), "empPbNo", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), "empType", 1);
		BaseCrit.addEmpDC(dc, "emp", emp);
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_SHIFT_TYPE).toString(), "eq",
				new Integer[] { Integer.valueOf(3) });
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, employee.getEmpDeptNo());
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { employee.getEmpLocationNo() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), Emptype.PROP_ID,
				new Object[] { employee.getEmpType() });
		dc.add(Restrictions.le((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_JOIN_DATE).toString(), dayTo));
		dc.add(Restrictions.or(Restrictions.eq((new StringBuilder()).append(
				"emp.").append(Employee.PROP_EMP_STATUS).toString(), Integer
				.valueOf(1)), Restrictions.and(Restrictions.eq(
				(new StringBuilder()).append("emp.").append(
						Employee.PROP_EMP_STATUS).toString(), Integer
						.valueOf(0)), Restrictions.ge((new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_TERMINATE_DATE)
				.toString(), dayFrom))));
	}

	private void generateEmpshiftList() {
		Map empshfitMap = convertListToMap(scheduleList);
		Employee currEmp = null;
		Empshift empshift = null;
		for (int i = 0; empList != null && i < empList.size(); i++) {
			currEmp = (Employee) empList.get(i);
			for (Date tempDate = dayFrom; tempDate.compareTo(dayTo) <= 0; tempDate = DateUtil
					.dateAdd(tempDate, 1)) {
				empshift = (Empshift) empshfitMap.get((new StringBuilder())
						.append(currEmp.getId()).append(
								DateUtil.formatDateToS(tempDate, "yyyy-MM-dd"))
						.toString());
				currEmp.getSsNameList()
						.add(
								empshift != null ? ((Object) (empshift
										.getEmpshiftShiftNo()
										.getAttsShortName())) : "");
			}

		}

	}

	private List generateTitleList() {
		List titleInfoList = new ArrayList();
		String day = null;
		int count = 0;
		Calendar cal = Calendar.getInstance();
		FieldOperate field = null;
		for (Date tempDate = dayFrom; tempDate.compareTo(dayTo) < 0;) {
			cal.setTime(tempDate);
			day = DateUtil.formatDateToS(tempDate, "yyyy-MM-dd").substring(8);
			day = Integer.parseInt(day) >= 10 ? day : day.substring(1);
			field = new FieldOperate((new StringBuilder())
					.append("ssNameList.").append(count).toString(), day,
					"String", null, 8);
			if (cal.get(7) == 7 || cal.get(7) == 1)
				field.setColBgColor(new java.awt.Color(255, 255, 0));
			titleInfoList.add(field);
			tempDate = DateUtil.dateAdd(tempDate, 1);
			count++;
		}

		return titleInfoList;
	}

	private Map convertListToMap(List list) {
		Map map = new HashMap();
		if (list == null || list.size() == 0)
			return map;
		Empshift shift;
		for (Iterator i$ = list.iterator(); i$.hasNext(); map.put(
				(new StringBuilder()).append(shift.getEmpshiftEmpNo().getId())
						.append(
								DateUtil.formatDateToS(shift.getEmpshiftDate(),
										"yyyy-MM-dd")).toString(), shift))
			shift = (Empshift) i$.next();

		return map;
	}

	public HashMap getEndDay(String year, String month) {
		HashMap map = new HashMap();
		Date dayFrom = null;
		Date dayTo = null;
		SysConfigManager dbConfigManager = DatabaseSysConfigManager
				.getInstance();
		Map dbMap = dbConfigManager.getProperties();
		String configValue = (String) dbMap.get("sys.examin.month.sum");
		String startMonthStr = configValue == null
				|| configValue.indexOf("-") <= -1 ? ""
				: configValue.split("-")[0];
		String startDayStr = configValue == null
				|| configValue.indexOf("-") <= -1 ? ""
				: configValue.split("-")[1];
		Integer startMonth = Integer.valueOf(startMonthStr == null
				|| startMonthStr.equals("") ? 0 : Integer
				.parseInt(startMonthStr));
		Integer startDay = Integer.valueOf(startDayStr == null
				|| startDayStr.equals("") ? 1 : Integer.parseInt(startDayStr));
		List tmpList = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		calendar.set(1, Integer.parseInt(year));
		calendar.set(2, Integer.parseInt(month) - 1);
		calendar.set(5, startDay.intValue());
		calendar.add(2, startMonth.intValue() - 1);
		int day = calendar.getActualMaximum(5);
		dayFrom = calendar.getTime();
		for (int i = 1; i <= day; i++) {
			tmpList.add(Integer.valueOf(calendar.get(7)));
			tmpList.add(Integer.valueOf(calendar.get(5)));
			calendar.add(5, 1);
		}

		dayTo = calendar.getTime();
		dayFrom = DateUtil.parseDateByFormat(DateUtil.formatDateToS(dayFrom,
				"yyyy-MM-dd"), "yyyy-MM-dd");
		dayTo = DateUtil.parseDateByFormat(DateUtil.formatDateToS(dayTo,
				"yyyy-MM-dd"), "yyyy-MM-dd");
		map.put("tmpList", tmpList);
		map.put("dayFrom", dayFrom);
		map.put("dayTo", dayTo);
		return map;
	}

	public Attendshift updateOneSchedule(String attdEmpId, String attdDutyDate,
			Attendshift attdShift) {
		if (!"0".equals(attdShift.getId()))
			attdShift = (Attendshift) attdShiftBo.loadObject(Attendshift.class,
					attdShift.getId(), null, new boolean[0]);
		String empIdStr = (new StringBuilder()).append(attdEmpId).append(",")
				.toString();
		String shift_day_str = (new StringBuilder()).append(attdShift.getId())
				.append(":").append(attdDutyDate.substring(8)).append("##")
				.toString();
		String msg = batchUpdateSchedule(attdDutyDate, attdDutyDate, empIdStr,
				shift_day_str);
		if (!"succ".equals(msg))
			attdShift.setFlag(msg);
		return attdShift;
	}

	public List getCommonShiftList(String idStr) {
		String empIds[] = idStr.split(",");
		List shiftList = attdShiftBo.getShiftIdsByRestrict(empIds);
		return shiftList;
	}

	public String batchUpdateSchedule(String startDate, String endDate,
			String empIdStr, String shift_day_str) {
		String message = "";
		String auth = DWRUtil.checkAuth("examinScheduleSearch", "execute");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		if (shift_day_str == null || shift_day_str.trim().length() == 0)
			return message;
		IEmpshiftBo empshiftBo = (IEmpshiftBo) getBean("empshiftBO");
		String empIdArr[] = empIdStr == null || empIdStr.indexOf(",") <= -1 ? null
				: empIdStr.split(",");
		Map dateMap = convertDayStr(startDate, endDate, shift_day_str);
		Date allDateArray[] = (Date[]) (Date[]) dateMap.get("alldate");
		String shiftdateArray[] = (String[]) (String[]) dateMap
				.get("shiftdate");
		HashMap existEmpshiftMap = empshiftBo.getEmpshiftList(empIdArr,
				allDateArray);
		Map allShiftMap = getAllShiftMap();
		List empshiftList = generateEmpshiftList(empIdArr, shiftdateArray,
				existEmpshiftMap, allShiftMap);
		List errorList = empshiftBo.validateShift(empshiftList,
				existEmpshiftMap);
		empshiftBo.checkShiftSchedule(errorList, empIdArr, empshiftList);
		if (errorList.size() > 0) {
			for (Iterator i$ = errorList.iterator(); i$.hasNext();) {
				String error = (String) i$.next();
				message = (new StringBuilder()).append(message).append(" ")
						.append(error).toString();
			}

			return message;
		}
		boolean updateFlag = attdShiftBo.batchUpdateEmpshift(empshiftList);
		if (message.trim().length() == 0 && updateFlag)
			message = "succ";
		return message;
	}

	private Map convertDayStr(String startDate, String endDate,
			String shift_day_str) {
		String shiftDayArr[] = shift_day_str == null
				|| shift_day_str.indexOf("##") <= -1 ? null : shift_day_str
				.split("##");
		Map result = new HashMap();
		Set shiftDateSet = new HashSet();
		String tempShiftId = null;
		String tempDate = null;
		String shiftDayTempArr[] = null;
		String dayArr[] = null;
		for (int i = 0; i < shiftDayArr.length; i++) {
			shiftDayTempArr = shiftDayArr[i].split(":");
			tempShiftId = shiftDayTempArr[0];
			dayArr = shiftDayTempArr[1].split(",");
			for (int j = 0; j < dayArr.length; j++) {
				tempDate = attdShiftBo.getRealDate(startDate, endDate,
						dayArr[j]);
				shiftDateSet.add((new StringBuilder()).append(tempShiftId)
						.append(",").append(tempDate).toString());
			}

		}

		Date allDateArray[] = new Date[shiftDateSet.size()];
		String shiftDateArray[] = new String[shiftDateSet.size()];
		int index = 0;
		for (Iterator i$ = shiftDateSet.iterator(); i$.hasNext();) {
			String shiftdate = (String) i$.next();
			allDateArray[index] = DateUtil.parseDateByFormat(shiftdate
					.split(",")[1], "yyyy-MM-dd");
			shiftDateArray[index] = shiftdate;
			index++;
		}

		result.put("alldate", allDateArray);
		result.put("shiftdate", shiftDateArray);
		return result;
	}

	private Map getAllShiftMap() {
		List allShiftList = attdShiftBo.searchAttendShift(null, null);
		Map allShiftMap = new HashMap();
		Attendshift shift;
		for (Iterator i$ = allShiftList.iterator(); i$.hasNext(); allShiftMap
				.put(shift.getId(), shift))
			shift = (Attendshift) i$.next();

		return allShiftMap;
	}

	private List generateEmpshiftList(String empIds[], String shiftdateArray[],
			HashMap existEmpshiftMap, Map allShiftMap) {
		List empshiftList = new ArrayList();
		Employee currentEmp = getCurrentEmp();
		String empId = null;
		Date date = null;
		Empshift empshift = null;
		Empshift empshiftTemp = null;
		String[] shiftdayArr = null;
		Attendshift shift = null;
		for (int i = 0; i < empIds.length; ++i) {
			empId = empIds[i];
			for (int j = 0; j < shiftdateArray.length; ++j) {
				shiftdayArr = shiftdateArray[j].split(",");
				shift = (Attendshift) allShiftMap.get(shiftdayArr[0]);
				empshift = (Empshift) existEmpshiftMap.get(empId + ""
						+ shiftdayArr[1]);
				if (empshift != null) {
					try {
						empshiftTemp = (Empshift) BeanUtils.cloneBean(empshift);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (("0".equals(shiftdayArr[0])) && (empshift != null)) {
					empshiftTemp.setEmpshiftShiftNo(null);
					empshiftList.add(empshiftTemp);
				} else if ((!"0".equals(shiftdayArr[0])) && (empshift == null)) {
					try {
						date = DateUtil.parseDateByFormat(shiftdayArr[1],
								"yyyy-MM-dd");
					} catch (Exception e) {
						e.printStackTrace();
					}
					empshift = new Empshift();
					empshift.setEmpshiftEmpNo(new Employee(empId));
					empshift.setEmpshiftDate(date);
					empshift.setEmpshiftShiftNo(shift);
					empshift.setEmpshiftLastChangeBy(currentEmp.getEmpName());
					empshift.setEmpshiftLastChangeTime(new Date());
					empshiftList.add(empshift);
				} else if ((!"0".equals(shiftdayArr[0])) && (empshift != null)) {
					empshiftTemp.setEmpshiftShiftNo(shift);
					empshiftTemp.setEmpshiftLastChangeBy(currentEmp
							.getEmpName());
					empshiftTemp.setEmpshiftLastChangeTime(new Date());
					empshiftList.add(empshiftTemp);
				}
			}
		}
		Collections.sort(empshiftList, new Comparator() {
			public int compare(Object obj1, Object obj2) {
				Empshift shift1 = (Empshift) obj1;
				Empshift shift2 = (Empshift) obj2;
				return shift1.getEmpshiftDate().compareTo(
						shift2.getEmpshiftDate());
			}
		});
		return empshiftList;
	}

	private String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public String shiftSearch() {
		shiftList = attdShiftBo.searchAttendShift(null, Integer
				.valueOf(resultStatus));
		ILocationBO locBo = (ILocationBO) getBean("locationBO");
		locations = locBo.FindEnabledLocation();
		IDepartmentBO deptBo = (IDepartmentBO) getBean("departmentBO");
		departs = deptBo.FindEnabledDepartment();
		return "success";
	}

	public String insertShift(Attendshift attdShift) {
		if ((attdShift == null) || (attdShift.getAttsColor() == null)
				|| (attdShift.getAttsName() == null)
				|| (attdShift.getAttsShortName() == null)
				|| (attdShift.getAttsName().trim().length() == 0)
				|| (attdShift.getAttsSession() == null)) {
			return "fail";
		}
		List<Attendshift> existShifts = this.attdShiftBo.searchAttendShift(
				attdShift.getAttsShortName(), null);
		if (attdShift.getId() == null) {
			if ((existShifts != null) && (existShifts.size() > 0))
				return "[班次代码] 不允许重复！";
		} else if ((existShifts != null) && (existShifts.size() > 0)) {
			for (Attendshift shift : existShifts) {
				if (!shift.getId().equals(attdShift.getId())) {
					return "[班次代码] 不允许重复！";
				}
			}
		}
		if ((attdShift.getId() != null) && (!"".equals(attdShift.getId()))) {
			List shiftIndbList = this.attdShiftBo
					.searchAttendShiftById(attdShift.getId());
			Attendshift shiftIndb = ((shiftIndbList != null) && (shiftIndbList
					.size() > 0)) ? (Attendshift) shiftIndbList.get(0) : null;
			if ((shiftIndb != null)
					&& (!shiftIndb.getAttsSession().equals(
							attdShift.getAttsSession()))
					&& (this.attdShiftBo.isUsed(shiftIndb).booleanValue()))
				return "班次正在使用，不允许修改班次时间段！";
		}
		String info = checkAttsSession(attdShift.getAttsSession());
		if (!"SUCC".equals(info))
			return info;
		computeWorkingHours(attdShift.getAttsSession(), attdShift);
		String locIds = attdShift.getLocIdString();
		String deptIds = attdShift.getDeptIdString();
		String[] locIdArr = ((locIds != null) && (locIds.length() > 0)) ? locIds
				.split(",")
				: null;
		String[] deptIdArr = ((deptIds != null) && (deptIds.length() > 0)) ? deptIds
				.split(",")
				: null;
		String result = this.attdShiftBo.saveOrUpdateShift(attdShift, locIdArr,
				deptIdArr);
		if (!"succ".equals(result))
			return result;
		return "succ,"
				+ StringUtil.formatBDToS(attdShift.getAttsWorkingHour(),
						new String[] { "#0.0 " }) + ","
				+ attdShift.getAttsNightShift();
	}

	public String getRelaString(String shiftId) {
		List<Attendshiftareadept> asadList = this.attdShiftBo
				.getAsadListByShift(shiftId);
		String value = "";
		for (Attendshiftareadept asad : asadList) {
			if (asad.getAsadAreaId() != null) {
				value = value + asad.getAsadAreaId().getId() + ","
						+ asad.getAsadAreaId().getLocationName() + "##";
			}
			if (asad.getAsadDeptId() != null) {
				value = value + asad.getAsadDeptId().getId() + ","
						+ asad.getAsadDeptId().getDepartmentName() + "##";
			}
		}
		return value;
	}

	public String delShift(String id) {
		if (id != null && id.trim().length() > 0) {
			Attendshift attdShift = (Attendshift) attdShiftBo.loadObject(
					Attendshift.class, id, null, new boolean[0]);
			if (attdShiftBo.isUsed(attdShift).booleanValue())
				return "isused";
			if (attdShiftBo.deleteShift(attdShift))
				return "succ";
		}
		return "fail";
	}

	public String sortShift() {
		if (operationId != null && operationId.trim().length() > 0) {
			String idArray[] = operationId.split(",");
			attdShiftBo.saveSort(idArray);
		}
		return "success";
	}

	public String setDefault(String operationId, int isDefault) {
		try {
			if (isDefault == 0) {
				BigDecimal shiftHours = this.attdShiftBo
						.getShiftHours(operationId);
				if (shiftHours == null)
					return "fail";
				double hoursPerDay = ExaminDateUtil.getDefaultHoursPerDay();
				if (shiftHours.doubleValue() != hoursPerDay)
					return "notDefaultHours," + hoursPerDay;
				this.attdShiftBo.saveDefault(operationId, 1);
			}
			if (isDefault == 1)
				this.attdShiftBo.saveDefault(operationId, 0);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}

	public String setStatus(String shiftId, int status) {
		int insertedStatus = status != 1 ? 1 : 0;
		attdShiftBo.saveStatus(shiftId, insertedStatus);
		return "succ";
	}

	public int getGlobalDefaultCount() {
		return attdShiftBo.getGlobalDefaultCount();
	}

	public void computeWorkingHours(String sessionStr, Attendshift attdShift) {
		Date date = DateUtil.convDateTimeToDate(new Date());
		Date lastESArr[] = ExaminDateUtil.getShiftArr(date, sessionStr);
		int minuteInShift = ExaminDateUtil.minutesInShift(lastESArr[0],
				lastESArr[1], date, sessionStr);
		BigDecimal workingHours = new BigDecimal(MyTools.round(
				minuteInShift / 60, 1));
		attdShift.setAttsWorkingHour(workingHours);
		if (lastESArr[1].getTime() - date.getTime() > 0x5265c00L)
			attdShift.setAttsNightShift(new Integer(1));
		else
			attdShift.setAttsNightShift(new Integer(0));
	}

	public String checkAttsSession(String sessionStr) {
		if (sessionStr == null || sessionStr.trim().length() == 0)
			return "\u73ED\u6B21\u65F6\u95F4\u6BB5\u4E3A\u7A7A\uFF0C\u683C\u5F0F\u5F62\u5982\uFF1A08:00-12:00,13:00-17:00";
		String str[] = sessionStr.split(",");
		String hour[] = null;
		for (int i = 0; str != null && i < str.length; i++) {
			hour = str[i] == null || str[i].indexOf("-") <= -1 ? null : str[i]
					.split("-");
			if (hour == null || hour.length < 2
					|| !hour[0].matches("\\d{1,2}:\\d{1,2}")
					|| !hour[1].matches("\\d{1,2}:\\d{1,2}"))
				return "\u5DE5\u65F6\u65F6\u95F4\u6BB5\u683C\u5F0F\u4E0D\u6B63\u786E\uFF0C\u683C\u5F0F\u5F62\u5982\uFF1A08:00-12:00,13:00-17:00";
		}

		return "SUCC";
	}

	public double minusToHour(long minus) {
		SysConfigManager dbConfigManager = DatabaseSysConfigManager
				.getInstance();
		Map dbMap = dbConfigManager.getProperties();
		String config = (String) dbMap.get("sys.shift.card.calchour");
		String configArr[] = config == null || config.indexOf(",") <= -1 ? null
				: config.split(",");
		long half = 0L;
		long one = 0L;
		if (configArr != null) {
			one = Long.parseLong(configArr[0]);
			half = Long.parseLong(configArr[1]);
		}
		double result = minus / 60L;
		long temp = minus % 60L;
		if (half <= temp && temp < one)
			result += 0.5D;
		else if (one <= temp)
			result++;
		return result;
	}

	public String formatBD(BigDecimal input) {
		if (input == null) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#0.0 ");
			return df.format(input);
		}
	}

	public String getDepName(String id) {
		if (id == null || id == "")
			return "";
		for (Iterator iterator = departs.iterator(); iterator.hasNext();) {
			Department dep = (Department) iterator.next();
			if (id.equals(dep.getId()))
				return dep.getDepartmentName();
		}

		return "";
	}

	public String getEmpType(String typeId) {
		if (typeId == null)
			return "";
		for (Iterator iterator = empTypes.iterator(); iterator.hasNext();) {
			Emptype type = (Emptype) iterator.next();
			if (typeId.equals(type.getId()))
				return type.getEmptypeName();
		}

		return "";
	}

	public String getLocName(String id) {
		if (id == null || id == "")
			return "";
		for (Iterator iterator = locations.iterator(); iterator.hasNext();) {
			Location loc = (Location) iterator.next();
			if (id.equals(loc.getId()))
				return loc.getLocationName();
		}

		return "";
	}

	public String getEmp() {
		return emp;
	}

	public void setEmp(String emp) {
		this.emp = emp;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public List getDayList() {
		return dayList;
	}

	public void setDayList(List dayList) {
		this.dayList = dayList;
	}

	public List getDeparts() {
		return departs;
	}

	public void setDeparts(List departs) {
		this.departs = departs;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getScheduleList() {
		return scheduleList;
	}

	public void setScheduleList(List scheduleList) {
		this.scheduleList = scheduleList;
	}

	public Date getAttdDutyDate1() {
		return attdDutyDate1;
	}

	public void setAttdDutyDate1(Date attdDutyDate1) {
		this.attdDutyDate1 = attdDutyDate1;
	}

	public Date getAttdDutyDate2() {
		return attdDutyDate2;
	}

	public void setAttdDutyDate2(Date attdDutyDate2) {
		this.attdDutyDate2 = attdDutyDate2;
	}

	public Attendshift getAttdShift() {
		return attdShift;
	}

	public void setAttdShift(Attendshift attdShift) {
		this.attdShift = attdShift;
	}

	public Date getDefaultDate() {
		return defaultDate;
	}

	public void setDefaultDate(Date defaultDate) {
		this.defaultDate = defaultDate;
	}

	public String getEmpIdString() {
		return empIdString;
	}

	public void setEmpIdString(String empIdString) {
		this.empIdString = empIdString;
	}

	public List getShiftList() {
		return shiftList;
	}

	public void setShiftList(List shiftList) {
		this.shiftList = shiftList;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getOperationId() {
		return operationId;
	}

	public void setOperationId(String operationId) {
		this.operationId = operationId;
	}

	public List getEmpList() {
		return empList;
	}

	public void setEmpList(List empList) {
		this.empList = empList;
	}

	public String[] getEmpId() {
		return empId;
	}

	public void setEmpId(String empId[]) {
		this.empId = empId;
	}

	public String getEmpNameString() {
		return empNameString;
	}

	public void setEmpNameString(String empNameString) {
		this.empNameString = empNameString;
	}

	public List getDaysList() {
		return daysList;
	}

	public void setDaysList(List daysList) {
		this.daysList = daysList;
	}

	public List getWeeksList() {
		return weeksList;
	}

	public void setWeeksList(List weeksList) {
		this.weeksList = weeksList;
	}

	public Date getDayFrom() {
		return dayFrom;
	}

	public void setDayFrom(Date dayFrom) {
		this.dayFrom = dayFrom;
	}

	public Date getDayTo() {
		return dayTo;
	}

	public void setDayTo(Date dayTo) {
		this.dayTo = dayTo;
	}

	public String getDayFlag() {
		return dayFlag;
	}

	public void setDayFlag(String dayFlag) {
		this.dayFlag = dayFlag;
	}

	public String getMonthFlag() {
		return monthFlag;
	}

	public void setMonthFlag(String monthFlag) {
		this.monthFlag = monthFlag;
	}

	public String getDayFrom1() {
		return dayFrom1;
	}

	public void setDayFrom1(String dayFrom1) {
		this.dayFrom1 = dayFrom1;
	}

	public String getDayTo1() {
		return dayTo1;
	}

	public void setDayTo1(String dayTo1) {
		this.dayTo1 = dayTo1;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public List getEmpTypes() {
		return empTypes;
	}

	public void setEmpTypes(List empTypes) {
		this.empTypes = empTypes;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public int getResultStatus() {
		return resultStatus;
	}

	public void setResultStatus(int resultStatus) {
		this.resultStatus = resultStatus;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getYearMonth() {
		return yearMonth;
	}

	public void setYearMonth(String yearMonth) {
		this.yearMonth = yearMonth;
	}

	private static final long serialVersionUID = 1L;
	private List scheduleList;
	private List empList;
	private Pager page;
	private String emp;
	private Employee employee;
	private String empId[];
	private String yearMonth;
	private List dayList;
	private List departs;
	private List locations;
	private List empTypes;
	private String operation;
	private String operationId;
	private List shiftList;
	private String empIdString;
	private String empNameString;
	private Attendshift attdShift;
	private Date attdDutyDate1;
	private Date attdDutyDate2;
	private Date defaultDate;
	private IAttendshiftBO attdShiftBo;
	private List daysList;
	private List weeksList;
	private Date dayFrom;
	private Date dayTo;
	private String dayFrom1;
	private String dayTo1;
	private String monthFlag;
	private String dayFlag;
	private String flag;
	private String searchOrExport;
	private String outmatchModelId;
	private String outputIoName;
	private int resultStatus;
}
